/**
* Copyright (c) By zengqh.
*
* This program is just for fun or demo, in the hope that it  
* will be useful, you can redistribute it and/or modify freely.
*
* Time: 2013/05/29
* File: enn_resource_manager.h
* Blog: http://www.cnblogs.com/zengqh/
**/

#pragma once

#include "enn_resource.h"

namespace enn
{
class ResourceManager;

//////////////////////////////////////////////////////////////////////////
class ResAutoCreatorBase : public AllocatedObject, public IResourceCreator
{
public:
	virtual ~ResAutoCreatorBase() {}

	virtual void release();
	virtual bool prepareResource( Resource* res, void* context );

public:
	void setBasePath( const String& basePath ) { m_basePath = basePath; }
	const String& getBasePath() const { return m_basePath; }

	virtual String getLocateFileName( Resource* res ) const;

protected:
	virtual bool _prepareResourceImpl( Resource* res, MemoryBuffer& buff ) = 0;

protected:
	String m_basePath;
};

//////////////////////////////////////////////////////////////////////////
struct IResourceScheduler
{
	virtual void release() = 0;
	virtual void load( Resource* res, bool async ) = 0;
	virtual void unload( Resource* res, bool async ) = 0;
	virtual void checkLoad( Resource* res ) = 0;
	virtual void releaseRes( Resource* res ) = 0;
};

class DefaultResourceScheduler : public AllocatedObject, public IResourceScheduler, public IMsgQueueListener
{
public:
	DefaultResourceScheduler();
	virtual ~DefaultResourceScheduler();

public:
	void setCheckLoadAsync( bool b ) { m_checkLoadAsync = b; }

public:
	// IResourceScheduler
	virtual void release();
	virtual void load( Resource* res, bool async );
	virtual void unload( Resource* res, bool async );
	virtual void checkLoad( Resource* res );
	virtual void releaseRes( Resource* res );

	// IMsgQueueListener
	virtual void onWorkImpl( MsgQueueBase* mq, Message* msg );
	virtual void onCancelMsg( MsgQueueBase* mq, Message* msg );

protected:
	bool m_checkLoadAsync;
};

}